﻿////////////////////////////////////////////////////////////////////////////////////////////////////
//
//  © 2012 Stanislav S. Yarmonov.
//
//  TODO:LICENSE
//     
////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Data;

namespace Pandora.Data.Common
{
    ////////////////////////////////////////////////////////////////////////////////////////////////
    /// <summary>
    /// Database factory.
    /// </summary>
    /// <remarks>
    /// Database factory is the starting point to simplify database object creation. As a sample
    /// I recommend to create DbInfo.cs class next to AssemblyInfo.cs with content below:
    /// <example>
    /// using System;
    /// using System.Data;
    /// using System.Data.SqlClient;
    /// using Pandora.Data;
    /// 
    /// internal class Db : Pandora.Data.Common.DbFactory
    /// {
    /// 
    /// }
    /// and register "Default" connection string in application configuration file.
    /// </example>
    /// </remarks>
    ////////////////////////////////////////////////////////////////////////////////////////////////
    public class DbFactory
    {
        #region -- Constants -----------------------------------------------------------------------
        private const string CONFIG_DEFAULT = "Default";
        #endregion ---------------------------------------------------------------------------------

        private static object _Locker = new object();

        private static DbConnectionFactory _Default = null;
        ////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Connection factory for default database.
        /// </summary>
        ////////////////////////////////////////////////////////////////////////////////////////////
        public static DbConnectionFactory Default
        {
            get
            {
                //TODO:Use generic double-locking pattern.
                if (_Default == null)
                {
                    lock (_Locker)
                    {
                        if (_Default == null)
                        {
                            _Default = DbConnectionFactory.Config(DbFactory.CONFIG_DEFAULT);
                        }
                    }
                }

                return _Default;
            }
        }

        ////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Stored procedure creation routine.
        /// </summary>
        /// <param name="name">
        /// Stored procedure name.
        /// </param>
        /// <returns>
        /// Returns database command class for stored procedure.
        /// </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////
        public static IDbCommand Procedure(string name)
        {
            return DbFactory.Default.Procedure(name);
        }

        ////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Table access command creation routine.
        /// </summary>
        /// <param name="name">
        /// Table name.
        /// </param>
        /// <returns>
        /// Returns database command class for table.
        /// </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////
        public static IDbCommand Table(string name)
        {
            return DbFactory.Default.Table(name);
        }

        ////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Database query command creation routine.
        /// </summary>
        /// <param name="text">
        /// Query text.
        /// </param>
        /// <returns>
        /// Returns database command class for query.
        /// </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////
        public static IDbCommand Query(string text)
        {
            return DbFactory.Default.Query(text);
        }
    }
}
